//
//  XFMainViewController.swift
//  SinaBlog
//
//  Created by macs on 15/11/8.
//  Copyright © 2015年 BlackTail. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    //由于点击事件是基于 OC 机制,所以加上 objc
    @objc private func compBtnDidClick () {
        print("点击了compBtn   \(__FUNCTION__)")
        
        let nav = UINavigationController(rootViewController: ComposeViewController())
        
        presentViewController(nav, animated: true, completion: nil);
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        //重写tabbar, 然后通过 kvc 设置只读的tabbar为重写的 tabbar, 然后修改其属性
        let maintabBar = MainTabBar()
        setValue(maintabBar, forKey: "tabBar")
        //        print(tabBar.classForCoder)
        //为自定义按钮compBtn 添加点击事件
        maintabBar.compBtn.addTarget(self, action: "compBtnDidClick", forControlEvents: .TouchUpInside)
        addSubViews()
    }
    private func addSubViews() {
        
        addChildViewController(HomeTableViewController(), title: "首页",imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息",imageName: "tabbar_message_center")
        addChildViewController(DiscoverTableViewController(), title: "发现",imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我",imageName: "tabbar_profile")
    }
    
    private func addChildViewController(vc: UIViewController, title :String ,imageName : String) {
        //
        //        let nav = UINavigationController(rootViewController:vc)
        //设置新的navgationcontroller
        let nav = BaseNavViewController(rootViewController:vc)
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        //第一种设置选中状态下的图片文字
        //        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //        vc.tabBarItem.([NSForegroundColorAttributeName : UIColor.orangeColor() ], forState: .Selected)
        //第二种,设置 tinecolor
        // tabBar.tintColor = UIColor.orangeColor()
        addChildViewController(nav)
    }
    
}
